package com.blog.blog.service.oss.impl;

import com.blog.blog.common.PageResponse;
import com.blog.blog.mapper.OssFileMapper;
import com.blog.blog.service.ArticleService;
import com.blog.blog.service.ResumeService;
import com.blog.blog.service.oss.OssAdminService;
import com.blog.blog.service.oss.OssService;
import com.blog.blog.util.ContentImageUtils;
import com.blog.blog.vo.OssFileVO;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class OssAdminServiceImpl implements OssAdminService {
    private final OssFileMapper ossFileMapper;
    private final OssService ossService;
    private final ArticleService articleService;
    private final ResumeService resumeService;

    public OssAdminServiceImpl(OssFileMapper ossFileMapper, OssService ossService, ArticleService articleService, ResumeService resumeService) {
        this.ossFileMapper = ossFileMapper;
        this.ossService = ossService;
        this.articleService = articleService;
        this.resumeService = resumeService;
    }

    @Override
    public PageResponse<OssFileVO> pageList(Integer status, String q, Integer pageNo, Integer pageSize) {
        int p = (pageNo == null || pageNo < 1) ? 1 : pageNo;
        int s = (pageSize == null || pageSize < 1) ? 10 : Math.min(pageSize, 100);
        int offset = (p - 1) * s;
        List<OssFileVO> records = ossFileMapper.listPage(status, q, offset, s);
        long total = ossFileMapper.count(status, q);
        return new PageResponse<>(total, records);
    }

    @Override
    public void deleteByKey(String key) {
        ossService.delete(key);
        ossFileMapper.deleteByKey(key);
    }

    @Override
    public int cleanupUnused(boolean verify) {
        List<OssFileVO> files = ossFileMapper.list(0);
        int count = 0;
        for (OssFileVO f : files) {
            if (!verify || ossService.exists(f.getKey())) {
                ossService.delete(f.getKey());
                ossFileMapper.deleteByKey(f.getKey());
                count++;
            }
        }
        return count;
    }

    @Override
    public void setStatus(String url, Integer status) {
        if (status != null && status == 1) ossFileMapper.markUsedByUrl(url);
        else ossFileMapper.markUnusedByUrl(url);
    }

    @Override
    public boolean exists(String key) {
        return ossService.exists(key);
    }

    @Override
    public Set<String> references(String type, Long id) {
        Set<String> refs = new HashSet<>();
        if ("article".equalsIgnoreCase(type)) {
            com.blog.blog.vo.ArticleDetailVO a = articleService.getArticleDetail(id);
            if (a != null) {
                if (a.getCoverImage() != null) refs.add(a.getCoverImage());
                refs.addAll(ContentImageUtils.extractImageUrls(a.getContent()));
            }
        } else if ("resume".equalsIgnoreCase(type)) {
            com.blog.blog.vo.ResumeDetailVO r = resumeService.getResumeByUserId(id);
            if (r != null) {
                if (r.getAvatar() != null) refs.add(r.getAvatar());
                if (r.getProjects() != null) {
                    for (com.blog.blog.vo.ProjectVO p : r.getProjects()) {
                        if (p.getImageUrl() != null) refs.add(p.getImageUrl());
                        if (p.getHighlights() != null) refs.addAll(ContentImageUtils.extractImageUrls(p.getHighlights()));
                    }
                }
            }
        }
        return refs;
    }
}


